﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Memcached.ClientLibrary;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Web.Caching;

namespace Memceched
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetDBData();
            }
            // 获得客户端实例
            //mc.EnableCompression = false;启动压缩
            //mc.Set("test1", "111111111111111111111111111111");  //存储数据到缓存服务器，这里将字符串"my value"缓存，key 是"test"
            
            //SockIOPool.GetInstance().Shutdown();  //关闭池， 关闭sockets

            DateTime dts1 = DateTime.Now;
            GetDBData();
            Label1.Text = (DateTime.Now - dts1).TotalMilliseconds.ToString();
            DateTime dts2 = DateTime.Now;
            MemcachedClient mc = new MemcachedClient();
            string key = "DataList";
            if (mc.KeyExists(key))
            {
                //DataSet ds = ;
                mc.Get(key);
            }
            Label2.Text = (DateTime.Now - dts2).TotalMilliseconds.ToString();
            DateTime dts3 = DateTime.Now;
            Cache.Get("CacheList");
            Label3.Text = (DateTime.Now - dts3).TotalMilliseconds.ToString();
        }

        protected void btnAddCache_Click(object sender, EventArgs e)
        {
            MemcachedClient mc = new MemcachedClient();
            mc.Set(txtKey.Text, txtContent.Text.Trim());
            //getServerId(char.Parse("test"),3);
        }

        public DataSet GetDBData()
        {
            SqlConnection con = new SqlConnection("Data Source=192.168.0.223;Initial Catalog=Transtar2008;Persist Security Info=True;User ID=sa;Password=bitauto123!");
            SqlDataAdapter sdpt = new SqlDataAdapter("select * from dbo.TranstarCarSource where tvaid=1332", con);
            DataSet ds = new DataSet();
            sdpt.Fill(ds);
            sdpt.Dispose();
            con.Dispose();

            MemcachedClient mc = new MemcachedClient();
            mc.Set("DataList", ds);

            Cache.Insert("CacheList", ds, null, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.AboveNormal, null);

            return ds;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
        //    DateTime dts = DateTime.Now;
        //    GridView1.DataSource = GetDBData();
        //    GridView1.DataBind();
        //    Label1.Text = (DateTime.Now - dts).TotalMilliseconds.ToString();
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
        //    DateTime dts = DateTime.Now;
        //    MemcachedClient mc = new MemcachedClient();
        //    GridView1.DataSource = mc.Get("DataList") as DataSet;
        //    GridView1.DataBind();
        //    Label1.Text = (DateTime.Now - dts).TotalMilliseconds.ToString();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {

        }

        //public unsafe int getServerId(char* key, int serverTotal)
        //{
        //    int c, hash = 0;
        //    while (c == *key++)
        //    {
        //        hash += c;
        //    }
        //    return hash % serverTotal;
        //}

    }
}
